# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=gflags
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.2
pkgrel=4
pkgdesc="Google's Commandline flags module for C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gflags.github.io/gflags/"
msys2_repository_url="https://github.com/gflags/gflags"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
checkdepends=("${MINGW_PACKAGE_PREFIX}-gtest")
source=("https://github.com/gflags/gflags/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-fix-link-library.patch
        0002-fix-static-libs.patch
        "${_realname}-2.2.2.patch")
sha256sums=('34af2f15cf7367513b352bdcd2493ab14ce43692d2dcd9dfc499492966c64dcf'
            '1bb0d61f8273ec487012636ca404686cb7589cefb9c57477f2a1e380746d92f7'
            '07dec6f4d117c226c1521de623e17180e9dee1b54e81a242fb8a974cf58726a7'
            'fa257fc8fb5fdef6267bf65b5355ccd967ca97d30672bddca55d64219ee848fd')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-fix-link-library.patch"
  patch -Np1 -i "${srcdir}/0002-fix-static-libs.patch"

  # https://github.com/gflags/gflags/pull/334
  patch -Np1 -i "${srcdir}/${_realname}-2.2.2.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    -DBUILD_STATIC_LIBS=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    "../${_realname}-${pkgver}"

  cmake --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cmake ../${_realname}-${pkgver} -DBUILD_TESTING=ON
  ${MINGW_PREFIX}/bin/cmake --build .
  ctest || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .
  INSTALL -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.txt
}
